package com.tweet_o_matic.models;

import android.content.Context;

import java.text.DateFormat;
import java.util.Date;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.tweet_o_matic.R;

public class Tweet {

	  public UUID id;
	  public String  message;
	  public Date    date;
	  public String  contact;
	  
	  private static final String JSON_ID           = "id"            ;
	  private static final String JSON_MESSAGE    	= "geolocation"   ; 
	  private static final String JSON_DATE         = "date"          ;
	  private static final String JSON_CONTACT       = "contact";  

	  public Tweet()
	  {
	    id = UUID.randomUUID();
	    this.date = new Date();
	  }
	 
	  public Tweet(JSONObject json) throws JSONException
	  {
		contact        = json.getString(JSON_CONTACT);  
	    id      = UUID.fromString(json.getString(JSON_ID));
	    message = json.getString(JSON_MESSAGE);
	    date    = new Date(json.getLong(JSON_DATE));
	  }
	  
	  public JSONObject toJSON() throws JSONException
	  {
	    JSONObject json = new JSONObject();
	    json.put(JSON_ID            , id.toString());  
	    json.put(JSON_MESSAGE   	, message);
	    json.put(JSON_DATE          , date.getTime());
	    json.put(JSON_CONTACT        , contact);
	    return json;
	  }
	  
	  
	  public String getDateString()
	  {
	    return "Registered: " + DateFormat.getDateTimeInstance().format(date);
	  }
	  
	  
	  public String getTweetReport(Context context)
	  {
	    

	    String dateFormat = "EEE, MMM dd";
	    String dateString = android.text.format.DateFormat.format(dateFormat, date).toString();
	    String prospectiveContact = contact;
	    if (contact == null)
	    {
	    	prospectiveContact = context.getString(R.string.fromUser);
	    }
	    else
	    {
	    	prospectiveContact = context.getString(R.string.fromUser, contact);
	    }
	    String report =  message + "\nDate: " + dateString + " " + prospectiveContact;
	    return report;
	  }
	  
	  
	  
	}